<template>
  <div class="risk app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item>
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%;">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum  - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="任务" align="center" prop="recommendNum" >
            邀请
      </el-table-column>
      <el-table-column label="门槛" align="center" prop="recommendNum" >
        <template slot-scope="scope">
          {{ scope.row.recommendNum}} 人
        </template> 
      </el-table-column>
      <el-table-column label="奖金" align="center" prop="bonus" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']">删除</el-button>
        </template>
      </el-table-column>
      <div class=""></div>
    </el-table>

    <pagination v-if="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 新增奖励 -->
    <el-dialog :title="title" :visible.sync="open" width="590px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="奖金" prop="bonus">
              <el-input type="number" v-model="form.bonus" :min="0" placeholder="请输入奖金" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="邀请人数" prop="recommendNum">
              <el-input type="number" v-model="form.recommendNum" :min="0" placeholder="请输入邀请人数" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="排序" prop="orderNum">
              <el-input type="number" v-model="form.orderNum" :min="0" placeholder="请输入排序" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getbonusTaskList , postBonusTask ,deleteBonusTask} from "@/api/system/rewards";

export default {
  name: "rewards",
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      title: '',
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        endCreateDate: undefined,
        startCreateDate: undefined,
        status: undefined,
        type: undefined,
      },
      // 表单校验
      rules: {
        bonus: [
          { required: true, message: "请输入奖金", trigger: "blur" },
        ],
        recommendNum: [
          { required: true, message: "请输入邀请人数", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "请输入奖励排序", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getbonusTaskList(this.queryParams).then(response => {
        this.list = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        num: undefined,
        amt: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增奖励";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          postBonusTask(this.form).then(response => {
              this.$modal.msgSuccess("新增奖励成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除这项任务奖励').then(function () {
        return deleteBonusTask(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>